'''
Created on 24-Apr-2013

@author: cdac
'''

from SMS.PreprocessSMS import PreprocessSMS
from Plotting.StatsPlot import StatsPlot
from Experiments.Exp import Exp
import numpy
from scipy import stats
from matplotlib.pyplot import over

class MsgSizeExp(Exp):
    '''
    classdocs
    '''
    def __init__(self, ham_source, spam_source):
        '''
        Constructor
        '''
        self.set_file_sources(ham_source, spam_source)
        self.init_data_sources()
        
    def experiment(self):
        pass
        
        
    def process_msgs_for_size(self, msgs, name):
        msg_size_raw = list()
        msg_size_prep = list()
        msg_size_no_sw = list()
    
        self.extract_ham_stats(msgs, name + "Sample")
        msg_size_raw = msg_size_exp.get_stats_for_hams().get_size_of_all_msgs()

        # Preprocess ham and spam by removing punctuations and
        # special characters
        processor = PreprocessSMS()
          
        msgs = processor.remove_puncts_and_special_chars(msgs)
        msgs = processor.convert_to_lowercase(msgs)

        self.extract_ham_stats(msgs, name + "SamplePrep")
        msg_size_prep= msg_size_exp.get_stats_for_hams().get_size_of_all_msgs()
        
        # Remove all stopwords from samples
        msgs = processor.remove_stopwords(msgs)
        
        # Recalculate Stats of samples without stopwords        
        self.extract_ham_stats(msgs, name + "HamSampleSW")
        
        msg_size_no_sw= self.get_stats_for_hams().get_size_of_all_msgs()
                
        print 35*'=' + name + " Stats" + 35*'='    
        print "\t"*5 + "Min" + "\tMax" + "\tAvg" + "\tMedian" + "\tMode"
        print '-'*80
        print "Raw " + name + " Message:\t\t\t", min(msg_size_raw), "\t", max(msg_size_raw), "\t%.2f" % numpy.mean(msg_size_raw), "\t", numpy.median(msg_size_raw), "\t", stats.mode(msg_size_raw)[0][0]
        print "Preprocessed " + name + " Message:\t\t", min(msg_size_prep), "\t", max(msg_size_prep), "\t%.2f" % numpy.mean(msg_size_prep), "\t", numpy.median(msg_size_prep), "\t", stats.mode(msg_size_prep)[0][0]
        print name + " Message without Stopwords:\t\t", min(msg_size_no_sw), "\t", max(msg_size_no_sw), "\t%.2f" % numpy.mean(msg_size_no_sw),  "\t", numpy.median(msg_size_no_sw), "\t", stats.mode(msg_size_no_sw)[0][0]
        print 'Most common words:'
        print self.get_stats_for_hams().get_word_freq()
        print 2*"\n"
        
        return msg_size_raw, msg_size_prep, msg_size_no_sw
    
    
    def print_stats(self, name, msg_size_raw, msg_size_prep, msg_size_no_sw):
        print 35*'=' + name + " Stats" + 35*'='    
        print "\t"*5 + "Min" + "\tMax" + "\tAvg" + "\tMedian" + "\tMode"
        print '-'*80
        print "Raw " + name + " Message:\t\t\t", min(msg_size_raw), "\t", max(msg_size_raw), "\t%.2f" % numpy.mean(msg_size_raw), "\t", numpy.median(msg_size_raw), "\t", stats.mode(msg_size_raw)[0][0]
        print "Preprocessed " + name + " Message:\t\t", min(msg_size_prep), "\t", max(msg_size_prep), "\t%.2f" % numpy.mean(msg_size_prep), "\t", numpy.median(msg_size_prep), "\t", stats.mode(msg_size_prep)[0][0]
        print name + " Message without Stopwords:\t\t", min(msg_size_no_sw), "\t", max(msg_size_no_sw), "\t%.2f" % numpy.mean(msg_size_no_sw),  "\t", numpy.median(msg_size_no_sw), "\t", stats.mode(msg_size_no_sw)[0][0]
        print 2*"\n"
        

if __name__ == '__main__':
 
    msg_size_exp = MsgSizeExp('../Data/Ham.csv', '../Data/Spam.csv')
#     msg_size_exp = MsgSizeExp('../Data/HamSmall.csv', '../Data/SpamSmall.csv')
 
    max_hams = 4000
    incremental_value=700
    exp_range = range(0, max_hams, incremental_value)
    
    overall_ham_stats = list()
    
    for sample in range(0, max_hams, incremental_value):
        print "Sample Range: ", sample, " to ", sample+incremental_value
        msgs = msg_size_exp.get_msgs_range_from_ham(sample, sample+incremental_value)    
        overall_ham_stats.append(msg_size_exp.process_msgs_for_size(msgs, "Ham"))
    
    sample = 0
    print "Sample Range: ", sample, " to ", sample+incremental_value
    msgs = msg_size_exp.get_msgs_range_from_spam(sample, incremental_value)    
    msgs = msg_size_exp.process_msgs_for_size(msgs, "Spam")
   
    for msg in msgs:
        print msgs.count("call")
   
#     statsPlot = StatsPlot()
#     statsPlot.size_plot(exp_range, ham_msg_size_raw, ham_msg_size_prep, ham_msg_size_no_sw, spam_msg_size_raw, spam_msg_size_prep, spam_msg_size_no_sw)